﻿using IniParser;
using IniParser.Model;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace GameSetting.Global
{
    class Globals
    {
        private static string iniFilePath = "jxqy.ini";
        private static Encoding localEncoding = Encoding.GetEncoding(936); //ini,script文件均为gb2312编码，若要国际化，需要修改为utf8
        public static Settings GameSetting = new Settings();
        public static Settings LoadSetting()
        {
            try
            {
                if (!File.Exists(iniFilePath))
                {
                    GameSetting = new Settings();
                    return GameSetting;
                }
                var parser = new FileIniDataParser();
                var data = parser.ReadFile(iniFilePath, localEncoding);
                if (data != null)
                {
                    var setting = data["Setting"];
                    int value = 0;
                                        
                    if (int.TryParse(setting["GameType"], out value))
                        GameSetting.Type = value;

                    if (int.TryParse(setting["FullScreen"], out value))
                        GameSetting.FullScreen = value > 0 ? 1 : 0;
                    if (int.TryParse(setting["Width"], out value))
                        GameSetting.Width = value;
                    if (int.TryParse(setting["Height"], out value))
                        GameSetting.Height = value;
                    if (int.TryParse(setting["IsUseThewWhenNormalRun"], out value))
                        GameSetting.IsUseThewWhenNormalRun = value > 0 ? 1 : 0;
                    if (int.TryParse(setting["RunSpeedFold"], out value))
                        GameSetting.RunSpeedFold = value;

                    if (float.TryParse(setting["SoundEffectVolume"], out float fv))
                    {
                        if (fv > 1) fv = 1;
                        else if (fv < 0) fv = 0;
                        GameSetting.SoundEffectVolume = (int)(fv * 100);
                    }
                    if (float.TryParse(setting["MusicVolume"], out fv))
                    {
                        if (fv > 1) fv = 1;
                        else if (fv < 0) fv = 0;
                        GameSetting.MusicVolume = (int)(fv * 100);
                    }

                    if (int.TryParse(setting["DebugMode"], out value))
                        GameSetting.DebugMode = value > 0 ? 1 : 0;

                    if (int.TryParse(setting["SaveLoadSelectionIndex"], out value))
                    {
                        GameSetting.SaveLoadSelectionIndex = value;
                    }
                    if (int.TryParse(setting["MaxMagicUnit"], out value))
                    {
                        GameSetting.MaxMagicUnit = value;
                    }
                }
                return GameSetting;
            }
            catch (Exception exception)
            {
                //no setting file, do nothing
                //throw exception;
                GameSetting = new Settings();
                return GameSetting;
            }
        }
        public static void SaveSetting()
        {
            try
            {
                IniData data;
                if (!File.Exists(iniFilePath))
                {
                    data = new IniData();
                }
                else
                {
                    data = new FileIniDataParser().ReadFile(iniFilePath, localEncoding);
                }
                var section = data["Setting"];
                if (section == null)
                {
                    data.Sections.AddSection("Setting");
                    section = data["Setting"];
                }

                section["GameType"] = GameSetting.Type.ToString();
                section["FullScreen"] = GameSetting.FullScreen.ToString();
                section["IsUseThewWhenNormalRun"] = GameSetting.IsUseThewWhenNormalRun.ToString();
                section["DebugMode"] = GameSetting.DebugMode.ToString();
                {
                    section["Width"] = GameSetting.Width.ToString();
                    section["Height"] = GameSetting.Height.ToString();
                    float volume = (float)Math.Round(GameSetting.SoundEffectVolume / 100.0, 2);
                    section["SoundEffectVolume"] = volume.ToString();
                    volume = (float)Math.Round(GameSetting.MusicVolume / 100.0, 2);
                    section["MusicVolume"] = volume.ToString();
                    section["RunSpeedFold"] = GameSetting.RunSpeedFold.ToString();
                }
                section["SaveLoadSelectionIndex"] = GameSetting.SaveLoadSelectionIndex.ToString();
                section["MaxMagicUnit"] = GameSetting.MaxMagicUnit.ToString();
                File.WriteAllText(iniFilePath, data.ToString(), localEncoding);
            }
            catch (Exception exception)
            {
            }
        }
        /// <summary>
        /// 获取当前操作系统类型
        /// </summary>
        /// <returns>OSPlatform</returns>
        private static OSPlatform GetOsType()
        {
            OSPlatform os =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? OSPlatform.Windows :
                RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? OSPlatform.Linux :
                RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? OSPlatform.OSX :
                OSPlatform.FreeBSD; // 其他BSD变种会返回FreeBSD
            return os;
        }
        public static string GetGamepath()
        {
            var ostype = GetOsType();
            var gamename = "";
            if (ostype == OSPlatform.Windows)
            {
                switch ((GameType)Globals.GameSetting.Type)
                {
                    case GameType.Sword2:
                        gamename = "Sword2.exe";
                        break;
                    case GameType.Sworda:
                        gamename = "Sworda.exe";
                        break;
                    case GameType.NewSword:
                        gamename = "NewSword.exe";
                        break;
                    default:
                        gamename = "Sword2.exe";
                        break;
                }
            }
            if (ostype == OSPlatform.Linux)
            {
                switch ((GameType)Globals.GameSetting.Type)
                {
                    case GameType.Sword2:
                        gamename = "Sword2";
                        break;
                    case GameType.Sworda:
                        gamename = "Sworda";
                        break;
                    case GameType.NewSword:
                        gamename = "NewSword";
                        break;
                    default:
                        gamename = "Sword2";
                        break;
                }

            }
            var gamepath = Path.Combine(Application.StartupPath, gamename);
            return gamepath;

        }
        /// <summary>
        /// 执行Linux程序
        /// </summary>
        /// <param name="programPath"></param>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public static void ExecuteProgram(string programPath)
        {
            // 验证文件存在性
            if (!File.Exists(programPath))
            {
                throw new FileNotFoundException($"目标程序不存在: {programPath}");
            }

        }
    }

}
